//*******************************日期方法******************//
var getTwoBit = function(n) {
  return (n > 9 ? '' : '0') + n
}
//格式化日期或获取今天完整日期
var getTime = function(value, format, split = '-') {
  if (value) {
    var date = new Date(value)
  } else {
    var date = new Date()
  }
  var y = date.getFullYear()
  var m = getTwoBit(date.getMonth() + 1)
  var d = getTwoBit(date.getDate())
  var h = getTwoBit(date.getHours())
  var min = getTwoBit(date.getMinutes())
  var s = getTwoBit(date.getSeconds())
  var partOne = [y, m, d].join(split)
  var partTwo = [h, min, s].join(':')
  if (format == 'ymd') {
    return partOne
  } else if (format == 'hms') {
    return partTwo
  } else {
    return partOne + ' ' + partTwo
  }
}
var getTimestar = function(value, format, split = '-') {
  if (value) {
    var date = new Date(value)
  } else {
    var date = new Date()
  }
  var y = date.getFullYear()
  var m = getTwoBit(date.getMonth() + 1)
  var d = getTwoBit(date.getDate() - 3)
  var h = getTwoBit(date.getHours())
  var min = getTwoBit(date.getMinutes())
  var s = getTwoBit(date.getSeconds())
  var partOne = [y, m, d].join(split)
  var partTwo = [h, min, s].join(':')
  if (format == 'ymd') {
    return partOne
  } else if (format == 'hms') {
    return partTwo
  } else {
    return partOne + ' ' + partTwo
  }
}
//获取7天前的时间
var getTime1 = function(value) {
  var now = new Date()

  var date = new Date(now.getTime() - 7 * 24 * 3600 * 1000)
  var y = date.getFullYear()
  var m = date.getMonth() + 1
  m = m < 10 ? '0' + m : m
  var d = date.getDate()
  d = d < 10 ? '0' + d : d
  var h = date.getHours()
  h = h < 10 ? '0' + h : h
  var minute = date.getMinutes()
  var second = date.getSeconds()
  minute = minute < 10 ? '0' + minute : minute
  second = second < 10 ? '0' + second : second
  if (value == 'qi') {
    return y + '-' + m + '-' + d
  } else {
    return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second
  }
}
var getTimeyear = function() {
  var date = new Date()
  var y = date.getFullYear() - 2
  var m = date.getMonth() + 1
  m = m < 10 ? '0' + m : m
  var d = date.getDate()
  d = d < 10 ? '0' + d : d
  var h = date.getHours()
  h = h < 10 ? '0' + h : h
  var minute = date.getMinutes()
  var second = date.getSeconds()
  minute = minute < 10 ? '0' + minute : minute
  second = second < 10 ? '0' + second : second
  return y + '-' + m + '-' + d
}

var getBeforeDay1 = function(value, format, split = '-') {
  if (value) {
    var date = new Date(value)
  } else {
    var date = new Date()
  }
  var y = date.getFullYear()
  var m = getTwoBit(date.getMonth() + 1)
  var d = getTwoBit(date.getDate() - 7)
  var h = getTwoBit(date.getHours())
  var min = getTwoBit(date.getMinutes())
  var s = getTwoBit(date.getSeconds())
  var partOne = [y, m, d].join(split)
  var partTwo = [h, min, s].join(':')
  if (format == 'ymd') {
    return partOne
  } else if (format == 'hms') {
    return partTwo
  } else {
    return partOne + ' ' + partTwo
  }
}

//获取前N天日期，第一个参数为 负数，为前几天日期，第二个参数为 正数，为后几天日期
var getBeforeDay = function(val, format, split) {
  var date = new Date()
  date.setTime(date.getTime() + 3600 * 1000 * 24 * parseInt(val))
  var beforeDay = getTime(date, format, split)
  return beforeDay
}

//isNull
var isNull = function(val) {
  if (!val && typeof val != 'undefined' && val != 0) {
    return true
  } else {
    return false
  }
}

//去除特殊字符
var encodestr = function(str) {
  var s = ''
  if (str.length === 0) {
    return
  }
  s = str.replace(/&/g, '')
  s = s.replace(/</g, '')
  s = s.replace(/>/g, '')
  s = s.replace(/ /g, '')
  s = s.replace(/\'/g, '')
  s = s.replace(/\"/g, '')
  s = s.replace(/#/g, '')
  return s
}
//获取前五十年的年份
var getBeforeYear = function() {
  var date = new Date()
  var y = date.getFullYear()
  var formatYearArray = []
  for (var i = 0; i < 60; i++) {
    formatYearArray.push(y - i)
  }
  return formatYearArray
}
//获取当前小时前二十四小时，并返回
var getNowBeforeHour = function() {
  var nowhour = new Date().getHours()
  let beforeHour = 23 - nowhour
  var beforeHourArr = []
  var todayHour = []
  for (let i = 0; i <= nowhour; i++) {
    let num = i
    num < 10 ? (num = '0' + num) : num
    todayHour.push(num)
  }
  for (let j = 0; j < beforeHour; j++) {
    let num = j
    let useNum = 23 - num
    useNum < 10 ? (useNum = '0' + useNum) : useNum
    beforeHourArr.push(useNum)
  }
  let timeHour = todayHour.reverse().concat(beforeHourArr)
  let newTimehour = []
  for (let k = 0; k < timeHour.length; k++) {
    let item = timeHour[k]
    item = String(item) + '时'
    newTimehour.push(item)
  }
  return newTimehour
}
//文字描述 xx月xx日xx时
var formatWordTime = function() {
  let nowTime = getTime()
  let beforeTime = getBeforeDay(-1)
  //当前时间
  let nowTimeSplit = nowTime.split(' ')
  let nowTimeOne = nowTimeSplit[0].split('-')
  let nowTimeTwo = nowTimeSplit[1].split(':')
  //前一天
  let beforeTimeSplit = beforeTime.split(' ')
  let beforeTimeOne = beforeTimeSplit[0].split('-')
  let beforeTimeTwo = beforeTimeSplit[1].split(':')
  let obj = {
    nowTime: nowTimeOne[1] + '月' + nowTimeOne[2] + '日' + nowTimeTwo[0] + '时',
    beforeTime:
      beforeTimeOne[1] +
      '月' +
      beforeTimeOne[2] +
      '日' +
      beforeTimeTwo[0] +
      '时',
  }
  return obj
}

function getDiffDay(date_1, date_2) {
  let totalDays, diffDate
  let myDate_1 = Date.parse(date_1)
  let myDate_2 = Date.parse(date_2)
  diffDate = Math.abs(myDate_1 - myDate_2)
  totalDays = Math.floor(diffDate / (1000 * 3600 * 24))
  return totalDays
}

function afterTime() {
  // 获取当前时间
  const date = new Date()

  // 将当前小时数加上24，即可得到24小时后的时间
  date.setHours(date.getHours() + 24)

  // 定义一个数组用于存储24小时内每个整点的时间
  const result = []

  // 循环遍历24小时内的每个整点，并将其推入数组中
  for (let i = 0; i < 24; i++) {
    const hour = date.getHours() // 获取当前小时数
    const minute = '00' // 固定取 00 分钟
    result.push(`${hour}:${minute}`) // 将格式化后的时间字符串推入数组
    date.setHours(date.getHours() + 1) // 每次循环将小时数加 1 小时
  }

  result[0] = '现在'

  return result
}

function getDayTimeByMinuteArr() {
  const result = []
  for (let i = 0; i < 24; i++) {
    for (let j = 0; j < 60; j++) {
      let useI = i < 10 ? '0' + i : i
      let useJ = j < 10 ? '0' + j : j
      let time = useI + ':' + useJ
      result.push(time)
    }
  }
  return result
}

export {
  getTime,
  getBeforeDay,
  isNull,
  getBeforeDay1,
  encodestr,
  getBeforeYear,
  getNowBeforeHour,
  formatWordTime,
  getDiffDay,
  getTimestar,
  afterTime,
  getTime1,
  getTimeyear,
  getDayTimeByMinuteArr,
}
